<?php


namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\GfActivity;
use app\api\model\GfActivityOrder;
use app\api\model\GrActivityOrder;
use app\api\model\SjActivityOrder;
use app\api\model\SjMsg;
use app\api\model\GrActivity;
use app\api\service\Token as TokenService;
use app\lib\exception\ParameterException;

class Activity extends BaseController
{
    protected $beforeActionList = [
        'checkExclusiveScope' => ['only' => 'getDataList']
    ];

    /**
     *  获取推荐首页活动(官方-商家-活动)
     */
    public function getDataList()
    {
        $time = time();
        $gf = GfActivity::where('recommend','eq',1)
            ->where('end_time','gt',$time)
            ->with('imgs.img')->select()->toArray();
        $sj = SjMsg::where('recommend','eq',1)
            ->with('imgs.img')->select()->toArray();
        $gr = GrActivity::where('recommend','eq',1)
            ->where('end_time','gt',$time)
            ->with('imgs.img')->select()->toArray();
        $arr = array_merge($gf,$sj,$gr);
        return arraySort($arr,'order_sort');
    }

    /**
     * 获取活动订单信息(官方-商家-活动)
     */
    public function getOrderList()
    {
        $userInfo = TokenService::judgeUser();
        $gf = GfActivityOrder::where('openid',$userInfo['openid'])
            ->order('id desc')
            ->select()->each(function (&$item){
                $item['activity_msg'] = json_decode($item['activity_msg'],true);
                $item['order_type'] = 1;
            })->toArray();
        $sj = SjActivityOrder::where('openid',$userInfo['openid'])
            ->with('items')
            ->order('id desc')
            ->select()->each(function (&$item){
                $item['activity_msg'] = json_decode($item['activity_msg'],true);
                $item['order_type'] = 2;
            })->toArray();
        $gr = GrActivityOrder::where('openid',$userInfo['openid'])
            ->order('id desc')
            ->select()->each(function (&$item){
                $item['activity_msg'] = json_decode($item['activity_msg'],true);
                $item['order_type'] = 3;
            })->toArray();
        $arr = array_merge($gf,$sj,$gr);
        return arraySort($arr,'pay_time');
    }

    /**
     * 删除活动订单
     */
    public function deleteOrder($id, $order_type)
    {
        $user = TokenService::judgeUser();
        if($order_type == 1) {
            $order = GfActivityOrder::get($id);
        } elseif($order_type == 2) {
            $order = SjActivityOrder::get($id);
        } elseif($order_type == 3) {
            $order = GrActivityOrder::get($id);
        }
        if(!$order) {
            throw new ParameterException([
                'msg' => [
                    'order' => '订单信息为空'
                ]
            ]);
        }
        if($order['status'] > 1) {
            throw new ParameterException([
                'msg' => [
                    'order' => '订单已支付'
                ]
            ]);
        }
        if($user['openid'] != $order['openid']) {
            throw new ParameterException([
                'msg' => [
                    'order' => '用户状态错误，请重试!'
                ]
            ]);
        }
        if($order_type == 1) {
            GfActivityOrder::destroy($id);
        } elseif($order_type == 2) {
            SjActivityOrder::destroy($id);
        } elseif($order_type == 3) {
            GrActivityOrder::destroy($id);
        }
        return writeJson(203,[],'删除成功!');
    }
}